/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.selectable;

import java.util.ArrayList;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Component used to enable mouse selection on DOM elements<br>
 * Jquery use for this behavior is <code>$('#id').selectable(options);</code><br>
 * With Wicket, you just need to create a JQSelectable Component
 * 
 * @author Benoit Bouchez
 * 
 */
public class JQSelectable extends WebMarkupContainer implements IJQPluginComponent
{
	/** JQuery plugin */
	private JQSelectablePlugin plugin;
	/** JQuery default options */
	private JQSelectableOptions options = new JQSelectableOptions();

	/** Selected component */
	private Component selected = null;
	/** Unselected component */
	private Component unselected = null;
	/** Selecting component */
	private Component selecting = null;
	/** Unselecting component */
	private Component unselecting = null;
	/** Selected items ids */
	private ArrayList<Component> selectedComponents = new ArrayList<Component>();

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 */
	public JQSelectable(String id, String key)
	{
		super(id);
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQSelectablePlugin.DEFAULT_KEY;
		}
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQSelectablePlugin(key);
		}
		this.plugin = (JQSelectablePlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(plugin.getKey()).attachComponentForDocumentReady(this);
	}

	/**
	 * Constructor
	 * 
	 * @param id
	 *            wicket component id
	 */
	public JQSelectable(String id)
	{
		this(id, null);
	}

	public JQSelectableOptions getOptions()
	{
		return options;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQSelectableOptions.class))
		{
			this.options = (JQSelectableOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQSelectable.class, options.getClass(), JQSelectableOptions.class);
		}
	}

	public JQSelectablePlugin getPlugin()
	{
		return plugin;
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQSelectablePlugin) plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return "$('#" + this.getMarkupId() + "')." + (reset ? "selectable('destroy')." : "") + options.getDocumentReadyOptions();
	}

	public String getAfterAjaxUpdateScript()
	{
		return "$('#" + this.getMarkupId() + "')." + options.getDocumentReadyOptions();
	}

	/**
	 * Enables this selectable<br>
	 * Appends a javascript call <code>$('#id').selectable('enable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void enable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('enable')");
	}

	/**
	 * Disables this selectable<br>
	 * Appends a javascript call <code>$('#id').selectable('disable')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void disable(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('disable')");
	}

	/**
	 * Refresh this selectable<br>
	 * Appends a javascript call <code>$('#id').selectable('refresh')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void refresh(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('refresh')");
	}

	/**
	 * Destroy this selectable<br>
	 * Appends a javascript call <code>$('#id').selectable('destroy')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void destroy(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('destroy')");
	}

	/**
	 * Toggles this selectable<br>
	 * Appends a javascript call <code>$('#id').selectable('toggle')</code> to the ajax target.
	 * 
	 * @param target
	 *            Ajax target
	 */
	public void toggle(AjaxRequestTarget target)
	{
		target.appendJavascript("$('#" + this.getMarkupId() + "').selectable('toggle')");
	}

	/**
	 * Gets a list of selected components
	 * 
	 * @return a list of wicket components
	 */
	public ArrayList<Component> getSelectedComponents()
	{
		return selectedComponents;
	}

	/**
	 * Sets the list of selected components
	 * 
	 * @param selectedComponents
	 *            a list of wicket components
	 */
	public void setSelectedComponents(ArrayList<Component> selectedComponents)
	{
		this.selectedComponents = selectedComponents;
	}

	/**
	 * Gets the selected component given by the expression <code>$(ui.selected);</code>.
	 * 
	 * @return the selected wicket component
	 */
	public Component getSelected()
	{
		return selected;
	}

	/**
	 * Sets the selected component
	 * 
	 * @param selected
	 *            the selected wicket component
	 */
	public void setSelected(Component selected)
	{
		this.selected = selected;
	}

	/**
	 * Gets the unselected component given by the expression <code>$(ui.unselected);</code>.
	 * 
	 * @return the unselected wicket component
	 */
	public Component getUnselected()
	{
		return unselected;
	}

	/**
	 * Sets the unselected component
	 * 
	 * @param unselected
	 *            the unselected wicket component
	 */
	public void setUnselected(Component unselected)
	{
		this.unselected = unselected;
	}

	/**
	 * Gets the selecting component given by the expression <code>$(ui.selecting);</code>.
	 * 
	 * @return the selecting wicket component
	 */
	public Component getSelecting()
	{
		return selecting;
	}

	/**
	 * Sets the selecting component
	 * 
	 * @param selecting
	 *            the selecting wicket component
	 */
	public void setSelecting(Component selecting)
	{
		this.selecting = selecting;
	}

	/**
	 * Gets the unselecting component given by the expression <code>$(ui.selecting);</code>.
	 * 
	 * @return the unselecting wicket component
	 */
	public Component getUnselecting()
	{
		return unselecting;
	}

	/**
	 * Sets the unselecting component
	 * 
	 * @param unselecting
	 *            the unselecting wicket component
	 */
	public void setUnselecting(Component unselecting)
	{
		this.unselecting = unselecting;
	}
}
